package in.srain.cube.util.general;

import androidx.annotation.NonNull;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Reversed<T> implements Iterable<T> {

    private final List<T> original;

    public Reversed(List<T> original) {
        this.original = original;
    }

    @NonNull
    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            final ListIterator<T> iterator = original.listIterator(original.size());

            public boolean hasNext() {
                return iterator.hasPrevious();
            }

            public T next() {
                return iterator.previous();
            }

            public void remove() {
                iterator.remove();
            }
        };
    }

    public static <T> Reversed<T> reversed(List<T> original) {
        return new Reversed<>(original);
    }
}
